package com.eu.ebpm.engine.cmd.pvm;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.entity.deploy.Transfer;
import com.eu.ebpm.engine.entity.enumm.TaskStatus;
import com.eu.ebpm.engine.entity.history.HisTask;
import com.eu.ebpm.engine.entity.runtime.TaskEntity;
import com.eu.ebpm.engine.exception.ErrorCode;
import com.eu.ebpm.engine.exception.WorkFlowException;
import com.eu.ebpm.engine.repository.HisTaskRepository;
import com.eu.ebpm.engine.repository.TaskRepository;

/**
 * 
 * @author 王晓华
 */
@Component
public class LaneTaskFinishCommand extends PvmCommand {

	@Autowired
	private TaskRepository taskRepository;
	
	@Autowired
	private HisTaskRepository hisTaskRepository;
	
	public void execute(Mission m, TaskEntity task, Map<String, Object> vs, Transfer nextTransfer) {

		// 1、删除当前和 task
		int count = taskRepository.delete(task.getId());

		if (count <= 0) {
			throw new WorkFlowException(ErrorCode.RU_ALREADY_COMPLETE);
		}

		// 2、更新历史表
		HisTask hisTask = new HisTask();
		hisTask.setId(task.getId());
		hisTask.setTransferName(nextTransfer.getName());
		hisTask.setTransferDesc(nextTransfer.getDesc());
		hisTask.setDueTime(new Date());
		hisTask.setRemark(task.getRemark());
		hisTask.setStatus(TaskStatus.SUCCESS.getCode());
		hisTask.setAssignee(task.getAssignee());
		hisTask.setAssigneeTime(task.getAssigneeTime());
		hisTaskRepository.updateSuccess(hisTask);

		super.insertHisTaskVariable(task.getId(), vs);
	}

}
